﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class appleader_result_viewResult : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //判断任务是否全部完成，没有则返回
            if (Request.QueryString[0] != null)
            {
                int projectid = int.Parse(Request.QueryString[0].ToString());
                if (!MasApp.BLL.Search.MissionAllFinish(projectid))
                {
                    MasApp.BLL.ResponseHelper.alert("尚有项目任务未完成1，不能生成结果！", "list.aspx");
                }

                //直接赋权是另外判断是否完成评权任务
                if (MasApp.BLL.Search.SearchProjectWeightType(projectid).Equals("直接赋权"))
                {
                    DataTable dt = MasApp.BLL.Search.WeightValue(projectid);
                    //直接赋权是判断准则评权是否完成
                    float allrulevalue = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr[1].ToString().Equals("准则"))
                        {
                            allrulevalue += Convert.ToSingle(dr[2].ToString());
                    //直接赋权下判断各指标评权是否完成
                            float allindivalue = 0;
                            foreach (DataRow dr1 in dt.Rows)
                            {
                                if (dr1[1].ToString().Equals("指标"))
                                {
                                    //Convert.ToInt32(dr1[0].ToString()) 是指标ID
                                    if (MasApp.BLL.Search.RuleID(Convert.ToInt32(dr1[0].ToString())) == int.Parse(dr[0].ToString()))
                                    {
                                        allindivalue += Convert.ToSingle(dr1[2].ToString());
                                    }
                                }
                            }
                            if ((allindivalue - 1F)>0.0000001F)
                            {
                                MasApp.BLL.ResponseHelper.alert("尚有项目任务未完成2，不能生成结果！", "list.aspx");
                            }
                        }
                    }
                    if ((allrulevalue - 1F)>0.0000001F)
                    {
                        MasApp.BLL.ResponseHelper.alert("尚有项目任务未完成3，不能生成结果！", "list.aspx");
                    }
                }
                MasApp.BLL.ResponseHelper.alert("所有项目任务已完成，正在生成结果，请点击。。");
                write();
            }
            else
            {
                MasApp.BLL.ResponseHelper.alert("非法链接！", "list.aspx");
            }


        }
    }

    protected void write()
    {
        Label lb = new Label();
        int projectid=int.Parse(Request.QueryString[0].ToString());
        //dt_weightvalue(elementid, elementtype, weightvalue)
        DataTable dt_weightvalue = MasApp.BLL.Search.WeightValue(projectid);
        
        //开始rule(ruleid, rulename)
        DataTable dt_rule = MasApp.BLL.Search.SearchRuleIDandName(projectid);
        //levelmission( levelmissionid )
        DataTable dt_levelmissionid = MasApp.BLL.Search.LevelMissionIDs(projectid);
        //用来保存目标值
        float value4 = 0;
        //这个是遍历准则
        foreach (DataRow dr1 in dt_rule.Rows)
        {
            lb = new Label();
            lb.Text = "<br />【准则】;" + dr1[1].ToString();
            lb.ID = "rule" + dr1[0].ToString();
            form1.Controls.Add(lb);
            float value3 = 0;
            //indicator( indicatorid, indicatorname)
            DataTable dt_indi = MasApp.BLL.Search.SearchIndicatorIDsandNames(Convert.ToInt32(dr1[0].ToString()));
            //遍历指标
            foreach (DataRow dr2 in dt_indi.Rows)
            {
                lb = new Label();
                lb.Text = "&nbsp;&nbsp;&nbsp;&nbsp;【指标】：" + dr2[1].ToString();
                form1.Controls.Add(lb);
                float value1 = 0;
                //遍历评级任务ID
                foreach (DataRow dr3 in dt_levelmissionid.Rows)
                {
                    float levelvalue = MasApp.DAL.Search.LevelValue(Convert.ToInt32(dr3[0].ToString()), Convert.ToInt32(dr2[0].ToString()));
                    value1 += levelvalue;
                }
                //求指标平均级值
                value1 = value1 / Convert.ToSingle(dt_levelmissionid.Rows.Count);
                lb = new Label();
                lb.Text = "&nbsp;&nbsp;&nbsp;&nbsp;[极值：" + value1.ToString() + "&nbsp;]";
                form1.Controls.Add(lb);
                //求指标的权值,假设是直接赋权的
                int weightmissionid = MasApp.BLL.Search.SearchWeightMissionID(projectid);
                int elementid = int.Parse(dr2[0].ToString());
                //查找当前指标的权值
                MasApp.Model.weight we = new MasApp.Model.weight();
                we.Elementid = elementid;
                we.Weightmissionid = weightmissionid;
                we.Elementtype = "指标";
                //得到权值
                float value2 = MasApp.BLL.Search.SearhWeightValue(we);
                lb = new Label();
                lb.Text = "；&nbsp;&nbsp;&nbsp;&nbsp;[权值：" + value2.ToString() + "]" +"<br />";
                form1.Controls.Add(lb);
                //得出准则级值
                value3 += value1 * value2;
            }
            //显示极值
            ((Label)form1.FindControl("rule" + dr1[0].ToString())).Text += "&nbsp;&nbsp;&nbsp;&nbsp;[极值：" + value3.ToString() + "&nbsp;]";
            //求当前准则的权值,假设是直接赋权的
            int weightmissionid2 = MasApp.BLL.Search.SearchWeightMissionID(projectid);
            int elementid2 = int.Parse(dr1[0].ToString());
            //查找当前准则的权值
            MasApp.Model.weight we2 = new MasApp.Model.weight();
            we2.Elementid = elementid2;
            we2.Weightmissionid = weightmissionid2;
            we2.Elementtype = "准则";
            //得到权值
            float value5 = MasApp.BLL.Search.SearhWeightValue(we2);
            //显示
            ((Label)form1.FindControl("rule" + dr1[0].ToString())).Text += "；&nbsp;&nbsp;&nbsp;&nbsp;[权值：" + value5.ToString() + "&nbsp;]" + "<br />";
            value4 += value5 * value3;
        }
        //value4 即是目标值
        //取得 level(levelid, levelname, judgement, levelvalue) 序列按levelvalue降序
        DataTable dt_levelvalue2 = MasApp.DAL.Search.Levelvalue2(projectid);
        //判断等级
        int levelid = 0;
        string levelname = "";
        //遍历等级值
        foreach (DataRow dr4 in dt_levelvalue2.Rows)
        {
            if (value4 >= Convert.ToSingle(dr4[3].ToString()))
            {
                levelid = Convert.ToInt32(dr4[0].ToString());
                levelname = dr4[1].ToString();
                break;
            }
            else
            {
                levelid = Convert.ToInt32(dr4[0].ToString());
                levelname = dr4[1].ToString();
            }
        }
        lb = new Label();
        lb.Text = "<br />当前项目【目标】的等级为：<font color=red>" + levelname + "&nbsp;&nbsp;&nbsp;[目标值为：" + value4.ToString() + "]</font>。";
        form1.Controls.Add(lb);
    }
}
